package cn.com.shopec.erp.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.ISupplierAccountsDao;
import cn.com.shopec.erp.base.entity.SupplierAccounts;
import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.base.service.ISupplierAccountsService;
import cn.com.shopec.erp.base.service.ISupplierInfoService;
import cn.com.shopec.erp.base.vo.SupplierAccountsVo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;

@Service
public class SupplierAccountsServiceImpl implements ISupplierAccountsService{

	@Resource
	private ISupplierAccountsDao supplierAccountsDao;
	
	@Resource
	private ISupplierInfoService  supplierInfoService;
	
	

/*	public SupplierAccounts getSupplierAccountsById(String supId) {
		
		DetachedCriteria de = supplierAccountsDao.createCriteria();
		de.add(Restrictions.eq("supplierInfo.id", supId));
		de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierAccounts> list = supplierAccountsDao.findByCriteria(de);
		if(list.size() == 1){
			return list.get(0);
		}
		return null;
	}*/

	@Override
	public SupplierAccounts getSupplierAccountsById(String code,String accountsType) {
		DetachedCriteria de = supplierAccountsDao.createCriteria();
		de.createAlias("supplierInfo", "supplierInfo").add(Restrictions.eq("supplierInfo.code", code));
		de.add(Restrictions.eq("accountsType", accountsType));
		de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierAccounts> list = supplierAccountsDao.findByCriteria(de);
		if(list.size() == 1){
			return list.get(0);
		}
		return null;
	}

	@Override
	@Transactional
	public void updateSupplierAccounts(String code,SupplierAccounts supplierAccounts) throws Exception {
		if(null!=code && LogicUtil.isNotNullAndEmpty(code))
		{
			   SupplierInfo sup = supplierInfoService.getSupplierInfoCode(code);
			   SupplierAccounts account = new SupplierAccounts();
			   account.setSupplierInfo(sup);
			   account.setAccountsType(supplierAccounts.getAccountsType());
		      List<SupplierAccounts> accountsList = getSupplierAccountsList(account);
		    if(null != accountsList && accountsList.size()>0)
		    {
			   SupplierAccounts accounts = accountsList.get(0);
			   if(accounts.getAccountsType().equals("1")){
					accounts.setAccountsTime(supplierAccounts.getAccountsTime());
				}
				if(accounts.getAccountsType().equals("2")){
					accounts.setAccountsWay(supplierAccounts.getAccountsWay());
					accounts.setAccountsTime(supplierAccounts.getAccountsTime());
					accounts.setPointsScale(supplierAccounts.getPointsScale());
				}
				 supplierAccountsDao.update(accounts);
		   }else{
			   SupplierAccounts sp = new SupplierAccounts();
			   if(supplierAccounts.getAccountsType().equals("1"))//购销
			   { 
				   sp.setAccountsTime(supplierAccounts.getAccountsTime());
			   }
			   if(supplierAccounts.getAccountsType().equals("2"))
			   {
                   sp.setAccountsWay(supplierAccounts.getAccountsWay())	;	
                   sp.setAccountsTime(supplierAccounts.getAccountsTime());
                   if(supplierAccounts.getAccountsWay().equals("1")){
                	   sp.setPointsScale(supplierAccounts.getPointsScale());
                   }
			   }
			   sp.setAccountsType(supplierAccounts.getAccountsType());
			   sp.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
			   sp.setSupplierInfo(sup);
			   supplierAccountsDao.save(sp);
		   }
		}
	}
	
	@Override
	public List<SupplierAccounts> getSupplierAccountsListByCode(String code) {
		DetachedCriteria de = supplierAccountsDao.createCriteria();
		de.createAlias("supplierInfo", "supplierInfo").add(Restrictions.eq("supplierInfo.code", code));
		de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierAccounts> list = supplierAccountsDao.findByCriteria(de);
		return list;
	}

	@Override
	public List<SupplierAccounts> getSupplierAccountsListByCode(String code, int buyType) {
		DetachedCriteria de = supplierAccountsDao.createCriteria();
		de.createAlias("supplierInfo", "supplierInfo").add(Restrictions.eq("supplierInfo.code", code));
		if (buyType == 1) {
			de.add(Restrictions.eq("accountsType", "1")); // 1:购销
		} else if (buyType == 2) {
			de.add(Restrictions.eq("accountsType", "2")); // 2:代销
		}
		de.addOrder(Order.desc("brandCode"));
		de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierAccounts> list = supplierAccountsDao.findByCriteria(de);
		return list;
	}

	@Override
	public List<SupplierAccounts> getSupplierAccountsList(SupplierAccounts supplierAccounts) {
		DetachedCriteria de = supplierAccountsDao.createCriteria();
		if(null !=supplierAccounts){
			if(null != supplierAccounts.getSupplierInfo() && LogicUtil.isNotNullAndEmpty(supplierAccounts.getSupplierInfo().getCode()))
			{
				de.createAlias("supplierInfo", "supplierInfo").add(Restrictions.eq("supplierInfo.code", supplierAccounts.getSupplierInfo().getCode()));
			}
			if(LogicUtil.isNotNullAndEmpty(supplierAccounts.getAccountsType()))
			{
				de.add(Restrictions.eq("accountsType", supplierAccounts.getAccountsType()));
			}
			if(StringUtil.isExist(supplierAccounts.getBrandCode())){
				de.add(Restrictions.eq("brandCode", supplierAccounts.getBrandCode()));
			}
			
			de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		}
		List<SupplierAccounts> list = supplierAccountsDao.findByCriteria(de);
		return list;
		
	}

	/***
	 * 根据品牌no查询所对应供应商信息
	 */
	@Override
	public List<SupplierAccountsVo> getSupplierAccountsListByNo(String no) {
		return supplierAccountsDao.getSupplierAccountsListByNo(no);
	}

}
